package com.ruoyi.mobile.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.common.core.utils.DateUtils;
import com.ruoyi.mobile.domain.*;
import com.ruoyi.mobile.mapper.IShAppointmentMapper;
import com.ruoyi.mobile.mapper.ISysDeptMapper;
import com.ruoyi.mobile.service.IShAppointmentService;
import com.ruoyi.mobile.utils.NftResponse;
import com.ruoyi.mobile.utils.Paginator;
import com.ruoyi.mobile.utils.QueryUtil;
import com.ruoyi.mobile.utils.ResponseConstants;
import com.ruoyi.system.api.req.ShAppointmentReq;
import com.ruoyi.system.api.resp.ShAppointmentResp;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class ShAppointmentServiceImpl extends ServiceImpl<IShAppointmentMapper, ShAppointmentEntity> implements IShAppointmentService {
    @Autowired
    private ISysDeptMapper sysDeptMapper;

    @Override
    public NftResponse<Paginator<ShAppointmentResp>> getDatingList(ShAppointmentReq query) {
        if (query.getPageNo() == null || query.getPageNo() <= 0) {
            return new NftResponse<>(ResponseConstants.ERROR_CODE, "入参页码错误", null);
        }
        if (query.getPageSize() == null || query.getPageSize() <= 0) {
            return new NftResponse<>(ResponseConstants.ERROR_CODE, "入参每页数量错误", null);
        }
        Paginator<ShAppointmentResp> paginator = new Paginator<ShAppointmentResp>(query.getPageNo(), query.getPageSize(), 0, new ArrayList<>());
        QueryWrapper<ShAppointmentEntity> wrapperQuery = new QueryWrapper<>();
        if (null!=query.getCreateBy()) {
            wrapperQuery.eq(QueryUtil.camelToUnderline("createBy"), query.getCreateBy());
        }
        if (null!=query.getProductType()&&query.getProductType()==0) {
            wrapperQuery.inSql("shop_id", "SELECT shop_id FROM sh_product WHERE product_type<=80");
        }else if (null!=query.getProductType()&&query.getProductType()==-1) {
            wrapperQuery.inSql("shop_id", "SELECT shop_id FROM sh_product WHERE product_type>90");
        }else if (null!=query.getProductType()&&query.getProductType()==-2) {
            wrapperQuery.inSql("shop_id", "SELECT shop_id FROM sh_product WHERE product_type>80 and product_type<=90");
        }else {
            wrapperQuery.inSql("shop_id", "SELECT shop_id FROM sh_product WHERE product_type="+query.getProductType());
        }
        wrapperQuery.orderByDesc("create_time");
        IPage<ShAppointmentEntity> page = new Page<>(query.getPageNo(),query.getPageSize());
        IPage<ShAppointmentEntity> pageRtn = this.baseMapper.selectPage(page,wrapperQuery);
        List<ShAppointmentEntity> list = pageRtn.getRecords();
        List<ShAppointmentResp> result = BeanUtil.copyToList(list, ShAppointmentResp.class);
        for (ShAppointmentResp obj:result) {
            SysDeptEntity shop = sysDeptMapper.selectById(obj.getShopId());
            obj.setShopName(shop.getDeptName());
            obj.setShopLogo(shop.getShopLogo());
        }
        paginator.setPageNo(query.getPageNo());
        paginator.setPageSize(query.getPageSize());
        paginator.setTotalDataNum(pageRtn.getTotal());
        paginator.setPageData(result);
        return new NftResponse<>(ResponseConstants.SUCCESS_CODE, "获取成功", paginator);
    }

    @Override
    public NftResponse<Integer> insertDatingInfo(ShAppointmentReq query) {
        if (null == query.getShopId() || query.getShopId() <= 0) {
            return new NftResponse<>(ResponseConstants.ERROR_CODE, "请输入所属商铺", 0);
        }
        if (null == query.getPhone() || "".equals(query.getPhone())) {
            return new NftResponse<>(ResponseConstants.ERROR_CODE, "请输入预约电话", 0);
        }
        ShAppointmentEntity entity = BeanUtil.copyProperties(query, ShAppointmentEntity.class);
        entity.setCreateTime(DateUtils.getNowDate());
        int rtn = this.baseMapper.insert(entity);
        return new NftResponse<>(ResponseConstants.SUCCESS_CODE, "预约成功", rtn);
    }

    @Override
    public NftResponse<Boolean> deleteDatingInfo(ShAppointmentReq query) {
        QueryWrapper<ShAppointmentEntity> wrapper = new QueryWrapper<>();
        wrapper.eq(QueryUtil.camelToUnderline("id"), query.getId());
        this.baseMapper.delete(wrapper);
        return new NftResponse<>(ResponseConstants.SUCCESS_CODE, "删除成功", true);
    }
}
